package com.hexu.event.view;

import com.hexu.event.entity.Attractions;
import com.hexu.event.util.StageUtil;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import static com.hexu.event.util.ServiceUtil.administratorView;
import static com.hexu.event.util.ServiceUtil.attractionsService;

public class SearchKeyView {

    //取出景点的所有信息
    private static List<Attractions> listAttractionsAllInformation = attractionsService.getAttractionsInformation();

    //获取景点数量
    private static int attractionsCount = attractionsService.attractionsNumber();

    String string;

    {
        string = getstring(string);
    }

    private List<String> words = Arrays.asList((string).split("[\\s，。“”]"));

    private SearchKeyView.InstantSearch<String> instantSearch = new SearchKeyView.InstantSearch<>(words, String::contains);

    private String getstring(String string){
        string = null;
        for (int i = 0;i<attractionsCount;i++){
            if(string != null)
                string = string +listAttractionsAllInformation.get(i).getIntroduce();
            else
                string = listAttractionsAllInformation.get(i).getIntroduce();
        }
        return string;
    }

    public void searchKeyWord (int visitorsOrUsersOrAdministrator,String phoneNumber,Stage stage) {
        //更新数据
        listAttractionsAllInformation = attractionsService.getAttractionsInformation();
        attractionsCount = attractionsService.attractionsNumber();
        string = getstring(string);
        words = Arrays.asList((string).split("[\\s，。“”]"));
        instantSearch = new SearchKeyView.InstantSearch<>(words, String::contains);

        ListView<String> listView = new ListView<>();

        //如果输入的字符长度大于0就进行搜索
        TextField textField = new TextField();
        textField.textProperty().addListener((observable, oldValue, newValue) -> {
            String trimed = newValue.trim();
            if (trimed.length() > 0) {
                doSearch(trimed,listView);
            }
        });

        VBox vBox = new VBox();
        vBox.getChildren().addAll(textField,listView);
        vBox.setPadding(new Insets(20));
        vBox.setSpacing(10);
        listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

        Stage primaryStage = new Stage();
        StageUtil.showStage(primaryStage,new Scene(vBox),500,400,"关键词搜索",false);
        //设置鼠标点击情况
        listView.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event)
            {
                //如果有所点击的话
                if(event.getButton() == MouseButton.PRIMARY && event.getClickCount() != 0) {

                    for(int i = 0;i<attractionsCount;i++){
                        if(listAttractionsAllInformation.get(i).getIntroduce().contains(listView.getItems().
                                get(listView.getSelectionModel().getSelectedIndex()).toString())){
                            stage.close();
                            primaryStage.close();
                            List<Attractions> list = new ArrayList<>();
                            list.add(listAttractionsAllInformation.get(i));
                            administratorView.view(stage,visitorsOrUsersOrAdministrator,phoneNumber,1,list);
                        }
                    }
                }

            }
        });

    }

    private void doSearch(String keyword,ListView<String> listview) {
        List<String> searchResult = instantSearch.search(keyword);
        listview.getItems().clear();
        listview.getItems().addAll(searchResult);
    }

    //使用泛型...这个是网上搜到的，BiFunction<T, String, Boolean>不怎么了解，网上又没说，我都不会搜了
    private class InstantSearch<T> {

        private Collection<T> tCollection;

        private BiFunction<T, String, Boolean> matcher;

        //前者存对象的信息（所有景点的），后者...
        public InstantSearch(Collection<T> tCollection, BiFunction<T, String, Boolean> matcher) {
            this.tCollection = tCollection;
            this.matcher = matcher;
        }

        //返回根据关键字搜索出来的信息
        public List<T> search(String keyword) {
            return (tCollection == null || matcher == null) ?
                    Collections.emptyList() :
                    tCollection.stream()
                            .filter(t -> matcher.apply(t, keyword))
                            .collect(Collectors.toList());
        }
    }
}
